/*
 * packet.h
 *
 *  Created on: Jun 5, 2012
 *      Author: adroaldo
 */

#ifndef PACKET_H_
#define PACKET_H_

/*network simulator 2*/
#include<packet.h>
#include "constants.h"

#define FC_LEN 1
#define USHORT_SIZE sizeof (unsigned short)
#define ULONG_SIZE sizeof (unsigned long)

typedef unsigned char token_type;

#define DELETED_PCKT                  0x00                /*binary representation 00000000*/

#define NORMAL_TOKEN                  0x01                /*binary representation 00000001*/

#define ELECTION_TOKEN                0x02		          /*binary representation 00000010*/
#define ELECTED_TOKEN                 0x03                /*binary representation 00000011*/

#define SOLICIT_SUCCESSOR             0x04                /*binary representation 00000100*/
#define SET_PREDECESSOR               0x05                /*binary representation 00000101*/
#define SET_SUCCESSOR                 0x06 		          /*binary representation 00000110*/

#define DATA_PCKT 			          0x07                /*binary representation 00000111*/
#define ACK_DATA_PCKT                 0x08                /*binary representation 00001000*/

#define VARAL_TOKEN			          0x09                /*binary representation 00001001*/

#define VARAL_ELECTION_TOKEN          0x0A	    	      /*binary representation 00001010*/
#define VARAL_ELECTED_TOKEN           0x0B			      /*binary representation 00001011*/

#define VARAL_SOLICIT_SUCCESSOR       0x0C			      /*binary representation 00001100*/
#define VARAL_SET_PREDECESSOR         0x0D			      /*binary representation 00001101*/
#define VARAL_SET_SUCCESSOR           0x0E				  /*binary representation 00001110*/

#define VARAL_DATA_TOKEN              0x0F				  /*binary representation 00001111*/
#define VARAL_ACK_DATA                0x010               /*binary representation 00010000*/

#define SELF_ADVERTISING              0x011               /*binary representation 00010001*/

#define HDR_WTRP_PKT(p) hdr_wtrp_pkt::access(p)

struct hdr_wtrp_pkt {

	nsaddr_t pkt_src_; /*Node which originated this packet*/
	u_int16_t pkt_len_; /*Packet length*/
	u_int8_t pkt_seq_num_; /*Packet sequence number*/

	inline nsaddr_t& pkt_src() {
		return pkt_src_;
	}
	inline u_int16_t& pkt_len() {
		return pkt_len_;
	}
	inline u_int8_t& pkt_seq_num() {
		return pkt_seq_num_;
	}

	static int offset_;
	inline static int& offset() {
		return offset_;
	}
	inline static hdr_wtrp_pkt* access(const Packet* p) {
		return (hdr_wtrp_pkt*) p->access(offset_);
	}
};

typedef struct {
	unsigned char FC;
	unsigned char RA[FIELD_LEN];
	unsigned char DA[FIELD_LEN];
	unsigned char SA[FIELD_LEN];
	unsigned short nOn;
	unsigned long seq;
	unsigned long gen_seq;
} token;

typedef struct {
	unsigned char FC;
	unsigned char RA[FIELD_LEN];
	unsigned char DA[FIELD_LEN]; /*Broadcast address*/
	unsigned char SA[FIELD_LEN];
} claim_token;

typedef struct {
	unsigned char FC;
	unsigned char RA[FIELD_LEN];
	unsigned char DA[FIELD_LEN];
	unsigned char SA[FIELD_LEN];
	unsigned short nOn;
	unsigned NEW_SUC[FIELD_LEN]; /*address of prospective successor*/

} s_successor;

typedef struct {
	unsigned char FC;
	unsigned char RA[FIELD_LEN];
	unsigned char DA[FIELD_LEN];
	unsigned char SA[FIELD_LEN];
	unsigned char NEW_SUCC[FIELD_LEN];
} set_suc;

typedef struct {
	unsigned char FC;
	unsigned char RA[FIELD_LEN];
	unsigned char DA[FIELD_LEN];
	unsigned char SA[FIELD_LEN];
	unsigned short nOn;
	unsigned long seq;
	unsigned long gen_seq;
} set_pre;

typedef struct {
	unsigned char FC;
	unsigned char RA[FIELD_LEN];
	unsigned char DA[FIELD_LEN];
	unsigned char SA[FIELD_LEN];

} del_token;

typedef struct {

} data;

typedef struct {
	unsigned char FC;
	unsigned char RA[FIELD_LEN];
	unsigned char DA[FIELD_LEN];
	unsigned char SA[FIELD_LEN];
} ack_pkt;

#endif /* PACKET_H_ */
